#include "tokenRes.h"
#include "jsonUtils.h"
#include "stringUtils.h"

TokenInfo::TokenInfo()
{

}

TokenInfo::~TokenInfo()
{
	
}

void TokenInfo::jsonDeserialize(JsonValue const& v)
{
	JsonUtils::loadLong(v, L"deviceId", deviceId);
	JsonUtils::loadLong(v, L"orgUnitId", orgUnitId);
	JsonUtils::loadStr(v, L"code", code);
	JsonUtils::loadStr(v, L"access_token", accessToken);
	JsonUtils::loadInt(v, L"expires_in", expiresIn);
	
	endClock = clock() + expiresIn * CLOCKS_PER_SEC;
}

std::string TokenInfo::getToken() const
{
	return StringUtils::Wstring2string(accessToken.c_str());
}

bool TokenInfo::nearestExpire() const
{
	return clock() + 10*60* CLOCKS_PER_SEC >= endClock;
}


JsonDoc& TokenRes::jsonDeserialize(const String& json)
{
	JsonDoc& doc = OperatorResponse::jsonDeserialize(json);
	const JsonValue::ConstMemberIterator& it = doc.FindMember(L"data");
	if (it != doc.MemberEnd() && it->value.IsObject()) {
		token.jsonDeserialize(it->value);
	}
	return doc;
}

TokenInfo TokenRes::getToken() const
{
	return token;
}
